package com.mjk.common.web.getway.utils;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import com.mjk.common.base.work.annotation.BeeApi;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import io.jsonwebtoken.Claims;
import org.apache.commons.collections.MapUtils;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 权限部分
 * @author Administrator
 *
 */
public class BeeRoleUtils {
    private String ROLE_MAGIC_API = "magicapi";

    private String ROLE_JIMU_REPORT = "jimureport";

    private String ROLE_PLUMELOG="plumelog";
    private BeeRoleUtils() {
    }
	
	public static final ThreadLocal<Boolean> isComplete =new ThreadLocal<>();
	
	public static final String ROLE = "beerole";
	
	/**
	 * 是否有权限
     */
    public static boolean havaRole(Method method) {
		BeeApi api = method.getAnnotation(BeeApi.class);
        //适配SaIgnore
        SaIgnore ignore = method.getAnnotation(SaIgnore.class);
        if (null != ignore) {
            return true;
        }
		if (null != api) {
			isComplete.set(api.setOnNull());
            if (ObjectUtils.isNotEmpty(api.role())) {
                return StpUtil.hasPermission(api.role());
            }
		}
        //兼容satoken
        SaCheckPermission permission = method.getAnnotation(SaCheckPermission.class);
        if (null != permission) {
            if (permission.mode().equals(SaMode.AND)) {//如果是and
                return StpUtil.hasPermissionAnd(permission.value());
            } else {
                return StpUtil.hasPermissionOr(permission.value());
            }

        }
        return true;
    }
	
}
